JavaScript

Array.prototype[@@unscopables] シンボルプロパティ

 編集:2022.08.20 by T.A.

@@unscopable シンボル プロパティには、ES2015 以前の ECMAScript 標準には含まれていないプロパティ名があります。それらのプロパティは、with 文のバインディングから除外されます。

Array.prototype[Symbol.unscopables]
解説

with バインディングから除外される既定の配列プロパティは次のとおりです:

自作オブジェクトに unscopables を設定する方法については、Symbol.unscopables を見てください。

with 環境内での使用

次のコードは、ES5 以下であれば正常に動作します。しかし、ECMAScript 2015 以降では Array.prototype.keys() メソッドが導入されました。つまり、with 環境内では、"keys" はメソッドであり、変数ではありません。ここが組込み @@unscopables の役立つ場所です。Array.prototype[@@unscopables] シンボルプロパティが使用されると、配列のいくつかのメソッドが with 文のスコープに入ることを防ぎます。

//コンソールにて
var keys = [];
with (Array.prototype) {
  keys.push('something');
}
D=Object.keys(Array.prototype[Symbol.unscopables]); //
// ["copyWithin", "entries", "fill", "find", "findIndex",
//  "includes", "keys", "values"]
var keys = [];
with (Array.prototype) {
  keys.push('something');
}
D=Object.keys(Array.prototype[Symbol.unscopables]); //=[object]:at,copyWithin,entries,fill,find,findIndex,flat,flatMap,includes,keys,values,findLast,findLastIndex
	//0=[string]:at
	//1=[string]:copyWithin
	//2=[string]:entries
	//3=[string]:fill
	//4=[string]:find
	//5=[string]:findIndex
	//6=[string]:flat
	//7=[string]:flatMap
	//8=[string]:includes
	//9=[string]:keys
	//10=[string]:values
	//11=[string]:findLast
	//12=[string]:findLastIndex

¶不明


テスト